import { CreationOptional, InferAttributes, InferCreationAttributes } from 'sequelize'
import {
    AllowNull,
    AutoIncrement,
    Column,
    CreatedAt,
    DataType,
    Index,
    Model,
    PrimaryKey,
    Table,
    Unique,
    UpdatedAt,
} from 'sequelize-typescript'

/**
 * 用户表
 */
@Table({ freezeTableName: true })
export class User extends Model<InferAttributes<User>, InferCreationAttributes<User>> {
    /**
     * 自增id
     */
    @PrimaryKey
    @AutoIncrement
    @Column(DataType.BIGINT({ unsigned: true }))
    declare id: CreationOptional<number>

    /**
     * 账号appid
     */
    @Unique('user')
    @AllowNull(false)
    @Column(DataType.STRING(50))
    declare app_id: string

    /**
     * 用户openid
     */
    @Unique('user')
    @AllowNull(false)
    @Column(DataType.STRING(100))
    declare openid: string

    /**
     * 开放平台unionid
     */
    @Index
    @Column(DataType.STRING(100))
    declare unionid: CreationOptional<string | null>

    /**
     * 昵称
     */
    @Column(DataType.STRING(50))
    declare nickname: string

    /**
     * 头像地址
     */
    @Column(DataType.STRING(255))
    declare avatar: string

    /**
     * 是否已关注
     */
    @Column(DataType.TINYINT({ length: 1 }))
    declare subscribed: CreationOptional<0 | 1 | null>

    /**
     * 创建时间
     */
    @CreatedAt
    @AllowNull(false)
    @Column(DataType.DATE)
    declare create_time: CreationOptional<Date>

    /**
     * 更新时间
     */
    @UpdatedAt
    @AllowNull(false)
    @Column(DataType.DATE)
    declare update_time: CreationOptional<Date>
}
